package com.ctgu.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.springboot.common.QueryPageParam;
import com.ctgu.springboot.entity.Student;
import com.ctgu.springboot.entity.StudentVO;
import com.ctgu.springboot.mapper.StudentMapper;
import com.ctgu.springboot.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wms
 * @since 2023-02-16
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Override
    public IPage<Student> studentPage(QueryPageParam query) {

        Integer id = (Integer) query.getParam().get("stu_id");
        String name = (String) query.getParam().get("stu_name");
        Integer schoolId = (Integer) query.getParam().get("sc_id");
        Integer teamId = (Integer) query.getParam().get("team_id");
        String phone = (String) query.getParam().get("stu_phone");

        Page<Student> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (id!=null) {
            lambdaQueryWrapper.like(Student::getStuId, id);
        }
        if (teamId!=null) {
            lambdaQueryWrapper.like(Student::getTeamId, teamId);
        }
        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Student::getStuName, name);
        }
        if (schoolId!=null) {
            lambdaQueryWrapper.eq(Student::getScId, schoolId);
        }
        if (StringUtils.isNotBlank(phone) && !"null".equals(phone)) {
            lambdaQueryWrapper.eq(Student::getStuPhone, phone);
        }
        return studentMapper.studentPage(page, lambdaQueryWrapper);
    }

    @Override
    public List<Student> getList(){
        return studentMapper.getList();
    }

    public StudentVO getInfo(Integer sc_id){
        return studentMapper.getInfo(sc_id);
    }

    public String getTeamName(Integer stu_id){
        return studentMapper.getTeamName(stu_id);
    }

    public String getTeam(String stu_name){
        return studentMapper.getTeam(stu_name);
    }
}
